﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using BitcoinCommon;
using BitcoinMarket.Market;
using BitcoinMarket.Data;


namespace BitcoinMarket
{
    public class MarketPrice
    {
        MainInfo _Comm;
        KorbitMarketPrice _Korbit;
        BTCeMarketPrice _BTCe;
        BitstampPrice _Bitstamp;

        public MarketPrice(MainInfo objComm)
        {
            _Comm = objComm;
            _Korbit = new KorbitMarketPrice(_Comm);
            _BTCe = new BTCeMarketPrice(_Comm);
            _Bitstamp = new BitstampPrice(_Comm);
        }

        public void RetrivedMarketPrice()
        {
            try
            {
                _Korbit.RetrivedOpenOrder();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

            try
            {
                _BTCe.RetrivedOpenOrder();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

            try
            {
                _Bitstamp.RetrivedData();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }

        public DataTable KorbitMarketAskTable()
        {
            return DataOpenOrderTable(eMarketOrderType.KORBIT_ASK).OrderTable;
        }

        public DataTable KorbitMarketBidTable()
        {
            return DataOpenOrderTable(eMarketOrderType.KORBIT_BID).OrderTable;
        }

        public DataTable BTCeMarketAskTable()
        {
            return DataOpenOrderTable(eMarketOrderType.BTCE_ASK).OrderTable;
        }

        public DataTable BTCeMarketBidTable()
        {
            return DataOpenOrderTable(eMarketOrderType.BTCE_BID).OrderTable;
        }

        private  DataMarketOpen DataOpenOrderTable(eMarketOrderType eOrder)
        {
            DataMarketOpen objReturn = null;

            switch (eOrder)
            {
                case eMarketOrderType.KORBIT_ASK:
                    objReturn = _Korbit.OpenAsk;
                    break;
                case eMarketOrderType.KORBIT_BID:
                    objReturn = _Korbit.OpenBid;
                    break;
                case eMarketOrderType.BTCE_ASK:
                    objReturn = _BTCe.OpenAsk;
                    break;
                case eMarketOrderType.BTCE_BID:
                    objReturn = _BTCe.OpenBid;
                    break;
            }

            return objReturn;
        }

        public Dictionary<string, string> MarketPriceData(eMarketPriceType eMarket)
        {
            Dictionary<string, string> oReturn = new Dictionary<string, string>();

            try
            {
                DataMarketPrice oItem = null;

                if (eMarket == eMarketPriceType.KORBIT)
                    oItem = _Korbit.MarketPrice();
                else
                    oItem = _Korbit.MarketPrice();

                oReturn.Add("timestamp", oItem.LastTime.ToString());
                oReturn.Add("LastPrice", oItem.LastPrice.ToString());
                oReturn.Add("AskPrice", oItem.AskPrice.ToString());
                oReturn.Add("BidPrice", oItem.BidPrice.ToString());
                oReturn.Add("LowPrice", oItem.LowPrice.ToString());
                oReturn.Add("HighPrice", oItem.HighPrice.ToString());
                oReturn.Add("Volume", oItem.Volume.ToString());

            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                oReturn.Add("timestamp", "");
                oReturn.Add("LastPrice", "");
                oReturn.Add("AskPrice", "");
                oReturn.Add("BidPrice", "");
                oReturn.Add("LowPrice", "");
                oReturn.Add("HighPrice", "");
                oReturn.Add("Volume", "");
            }

            return oReturn;
        }

        public string CurrentRate(string strBaseCurrency, string strTargetCurrency)
        {
            string strReturn = "0.0";

            string strURL = "";
            strURL = String.Format("http://www.webservicex.net/CurrencyConvertor.asmx/ConversionRate?FromCurrency={0}&ToCurrency={1}", strBaseCurrency.ToUpper(), strTargetCurrency.ToUpper());

            //string strData = Communication.GetRequestData(strURL);

            //if (strData.Length == 0)
            //    return strReturn;

            try
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(strURL);

                strReturn = xmlDoc.DocumentElement.InnerText;

            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }



            return strReturn;
        }

        public string CurrentRate()
        {
            return CurrentRate("USD", "KRW");
        }

        public double BitStampLastPrice()
        {
            return _Bitstamp.LastPrice;
        }

        public double BitStampHighPrice()
        {
            return _Bitstamp.HighPrice;
        }

        public double BitStampLowPrice()
        {
            return _Bitstamp.LowPrice;
        }

        public double BitStampVolume()
        {
            return _Bitstamp.Volume;
        }

        public double BitStampAskPrice()
        {
            return _Bitstamp.AskPrice;
        }

        public double BitStampBidPrice()
        {
            return _Bitstamp.BidPrice;
        }
            
    }



    public enum eMarketOrderType
    {
        KORBIT_ASK,
        KORBIT_BID,
        BTCE_ASK,
        BTCE_BID
    }

    public enum eMarketPriceType
    {
        KORBIT,
        BTCE
    }
}
